package com.study.goal.service.impl;

import cn.hutool.core.bean.BeanUtil;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.study.common.core.domain.PageQuery;
import com.study.common.core.page.TableDataInfo;
import com.study.common.utils.StringUtils;
import com.study.goal.domain.PerformanceRecords;
import com.study.goal.domain.bo.PerformanceRecordsBo;
import com.study.goal.domain.vo.PerformanceRecordsVo;
import com.study.goal.mapper.PerformanceRecordsMapper;
import com.study.goal.service.IPerformanceRecordsService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;


import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 成绩记录Service业务层处理
 *
 * @author MT
 * @date 2025-09-03
 */
@RequiredArgsConstructor
@Service
public class PerformanceRecordsServiceImpl implements IPerformanceRecordsService {

    private final PerformanceRecordsMapper baseMapper;

    /**
     * 查询成绩记录
     */
    @Override
    public PerformanceRecordsVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询成绩记录列表
     */
    @Override
    public TableDataInfo<PerformanceRecordsVo> queryPageList(PerformanceRecordsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PerformanceRecords> lqw = buildQueryWrapper(bo);
        Page<PerformanceRecordsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询成绩记录列表
     */
    @Override
    public List<PerformanceRecordsVo> queryList(PerformanceRecordsBo bo) {
        LambdaQueryWrapper<PerformanceRecords> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PerformanceRecords> buildQueryWrapper(PerformanceRecordsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PerformanceRecords> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getGoalId() != null, PerformanceRecords::getGoalId, bo.getGoalId());
        lqw.like(StringUtils.isNotBlank(bo.getExamName()), PerformanceRecords::getExamName, bo.getExamName());
        lqw.eq(bo.getScore() != null, PerformanceRecords::getScore, bo.getScore());
        lqw.eq(bo.getMaxScore() != null, PerformanceRecords::getMaxScore, bo.getMaxScore());
        lqw.eq(bo.getExamDate() != null, PerformanceRecords::getExamDate, bo.getExamDate());
        lqw.eq(StringUtils.isNotBlank(bo.getRemarks()), PerformanceRecords::getRemarks, bo.getRemarks());
        return lqw;
    }

    /**
     * 新增成绩记录
     */
    @Override
    public Boolean insertByBo(PerformanceRecordsBo bo) {
        PerformanceRecords add = BeanUtil.toBean(bo, PerformanceRecords.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改成绩记录
     */
    @Override
    public Boolean updateByBo(PerformanceRecordsBo bo) {
        PerformanceRecords update = BeanUtil.toBean(bo, PerformanceRecords.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PerformanceRecords entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除成绩记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
